<template>
  <el-main>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/company' }"
        >企业管理
      </el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/company/list' }"
        >企业列表
      </el-breadcrumb-item>
      <el-breadcrumb-item>新增企业</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="margin-top-20">
      <el-card shadow="never">
        <el-form :model="form" :rules="rules" label-width="80px" ref="form">
          <el-form-item class="m-input-400" label="企业名称" prop="companyName">
            <el-input v-model="form.companyName"></el-input>
          </el-form-item>
          <el-form-item class="m-input-400" label="企业电话" prop="mobile">
            <el-input v-model="form.mobile"></el-input>
          </el-form-item>
          <el-form-item
            class="m-input-400"
            label="企业账号"
            prop="companyAccount"
          >
            <el-input v-model="form.companyAccount"></el-input>
          </el-form-item>

          <el-form-item class="m-input-400" label="企业密码" prop="passwd">
            <el-input
              autocomplete="off"
              show-password
              type="password"
              v-model="form.passwd"
            />
          </el-form-item>
          <el-form-item class="m-input-400" label="确认密码" prop="repasswd">
            <el-input
              autocomplete="off"
              show-password
              type="password"
              v-model="form.repasswd"
            />
          </el-form-item>

          <el-form-item class="m-input-400" label="企业邮编" prop="postalCode">
            <el-input v-model="form.postalCode"></el-input>
          </el-form-item>
          <el-form-item class="m-input-400" label="企业地址" prop="address">
            <el-input v-model="form.address"></el-input>
          </el-form-item>
          <el-form-item class="m-input-400" label="企业简介" prop="remarks">
            <el-input type="textarea" v-model="form.remarks"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="submitForm('form')" type="primary"
              >立即创建
            </el-button>
            <el-button @click="cancelForm">取消</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </el-main>
</template>

<script>
export default {
  data() {
    const validateRePasswd = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.form.passwd) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };

    return {
      form: {
        companyAccount: null,
        companyName: null,
        passwd: null,
        repasswd: null,
        mobile: null,
        postalCode: null,
        address: null,
        remarks: null
      },
      rules: {
        companyAccount: [
          { required: true, message: "请输入企业账户", trigger: "none" },
          { min: 3, max: 25, message: "长度在 3 到 25 个字符", trigger: "none" }
        ],
        companyName: [
          { required: true, message: "请输入企业名称", trigger: "none" },
          { min: 3, max: 25, message: "长度在 3 到 25 个字符", trigger: "none" }
        ],
        passwd: [
          { required: true, message: "请输入密码", trigger: "none" },
          { min: 3, max: 25, message: "长度在 3 到 25 个字符", trigger: "none" }
        ],
        repasswd: [
          { required: true, message: "请再次输入密码", trigger: "none" },
          { validator: validateRePasswd, trigger: "none" }
        ]
      }
    };
  },
  mounted() {},
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let loadingInstance = this.$loading({ fullscreen: true });
          this.$ajax.company
            .add(this.form)
            .then(result => {
              if (result.code != 1) {
                this.$message({
                  message: result.msg,
                  type: "warning"
                });
              } else {
                this.$message({
                  type: "success",
                  message: "新增企业成功!"
                });
                this.$router.push("/company/list/");
              }
            })
            .catch(e => {
              console.log(e);
              this.$message({
                message: "系统繁忙，请稍后重试",
                type: "warning"
              });
            })
            .finally(function() {
              loadingInstance.close();
            });
        } else {
          return false;
        }
      });
    },
    cancelForm() {
      this.$router.push("/company/list/");
    }
  }
};
</script>
